package com.controller;


import com.pojo.User;
import com.service.FindUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@RequestMapping("/user")
@Controller
public class logincontroller {
    @Autowired
    private FindUserService finduservice;

    @RequestMapping("/login_show")
    public String login_show(){
        return "adminlogin";

    }

    /*
       接收前端表格传来user数据
       并执行查询校验
     */

    @RequestMapping("/login")
    public String loginpage(@RequestParam(value = "uname", required = false, defaultValue = "null") String uname, @RequestParam(value = "upassword", required = false, defaultValue = "null")String upassword , HttpSession session ,Model model){
        User user =new User();
        user.setUname(uname);
        user.setUpassword(upassword);
        System.out.println(user.toString());
        User user1=finduservice.adminLogin(user);
        if(user1==null) {
            model.addAttribute("error","用户名或密码不正确");
            return "adminlogin";
        }
        //将user内容存在会话中
        session.setAttribute("AdminUser",user1);
        return "loginmanager";
    }
    @RequestMapping("/index")
    public String welcome(){
        return "index";

    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return"adminlogin";



    }
}
